package com.hele.sts.pub;

import java.awt.print.Book;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.ResolutionSyntax;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.PrinterResolution;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;

public class PrintUtils {
	
	public static void printTiff(String filePath){
		final TiffPrinter myTif = new TiffPrinter(filePath);

		DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
		PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
		pras.add(MediaSizeName.ISO_A4);
	    PrintService printServices[] = PrintServiceLookup.lookupPrintServices(flavor, pras);

		
		PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
		PrintService printService = ServiceUI.printDialog(null, 0, 0,
				printServices, defaultService, null, pras);
		
		DocPrintJob job = defaultService.createPrintJob();
		PrintJobListener pjlistener = new PrintJobAdapter() {
			public void printDataTransferCompleted(PrintJobEvent e) {
				
				System.out.println("event:"+e.getPrintEventType());
				//e.getPrintJob().g
				//System.exit(0);
			}
		};

		// pras.add(new PrinterResolution(200,200,ResolutionSyntax.DPI));
		DocAttributeSet das = new HashDocAttributeSet();
		// das.add(new PrinterResolution(200,200,ResolutionSyntax.DPI));
		Doc doc = new SimpleDoc(myTif, flavor, das);
		try {
			job.addPrintJobListener(pjlistener);
			job.print(doc, pras);
		} catch (PrintException pe) {
			pe.printStackTrace();
		}
	}
	
	
	
	public static void test(String filepath) {
		File file =    new File(filepath);
		 //构建打印请求属性集
		PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();  
		//设置打印格式
	    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;          
	    
	    //查找所有的可用的打印服务    
	    PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
	    
	    //定义默认的打印服务(PrintService)
	    PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); 
	    
//	    //显示打印对话框
//	    PrintService service = ServiceUI.printDialog(null, 200, 200,
//	        printService, defaultService, flavor, pras);
//	    
//	    if( service != null ){
//	      
//	      try {
//	        //创建打印作业
//	        DocPrintJob job = service.createPrintJob();    
//	        //构建文件打印流
//	        FileInputStream fis = new FileInputStream(file);
//	        //构建文件属性
//	        DocAttributeSet das = new HashDocAttributeSet();
//	        
//	        Doc doc = new SimpleDoc(fis, flavor, das);
//	        
//	        job.print(doc, pras);
//	      } catch (FileNotFoundException e) {
//	        e.printStackTrace();
//	      } catch (PrintException e) {
//	        e.printStackTrace();
//	      }//end try
//	      
//	    }else{
	    	try {
		        //创建打印作业
		        DocPrintJob job = defaultService.createPrintJob();    
		        //构建文件打印流
		        FileInputStream fis = new FileInputStream(file);
		        //构建文件属性
		        DocAttributeSet das = new HashDocAttributeSet();
		        
		        Doc doc = new SimpleDoc(fis, flavor, das);
		        
		        job.print(doc, pras);
		      } catch (FileNotFoundException e) {
		        e.printStackTrace();
		      } catch (PrintException e) {
		        e.printStackTrace();
		      }//end try
//	    }
	}
	
}
